import java.awt.*;
import java.awt.event.*;

import javax.swing.SwingUtilities;

/*
 * mouse loo test, move indefinitily to anydirection
 */

public class MouselookTest extends GameCore implements MouseMotionListener, KeyListener
{

	public static void main(String[] args) {
		new MouselookTest().run();

	}
	private Image bgImage;
	private Robot robot;
	private Point mouseLocation;
	private Point centerLocation;
	private Point imageLocation;
	private boolean relativeMouseMode;
	private boolean isRecentering;
	
	public void init()
	{
		super.init();
		mouseLocation = new Point();
		centerLocation = new Point();
		imageLocation = new Point();
		
		relativeMouseMode = true;
		isRecentering = false;
		
		try{
			robot = new Robot();
			recenterMouse();
			mouseLocation.x = centerLocation.x;
			mouseLocation.y = centerLocation.y;
		}
		catch (AWTException ex){
			System.out.print("Couldnt create Robot");			
		}
		Window window = screen.getFullScreenWindow();
		window.addMouseMotionListener(this);
		window.addKeyListener(this);
		bgImage = loadImage("images/background.jpg");
				
	}
	
	public synchronized void draw(Graphics2D g)
	{
		int w = screen.getWidth();
		int h = screen.getHeight();

		//		check position
		imageLocation.x %= w;
		imageLocation.y %= screen.getHeight();
		
		if (imageLocation.x<0)
		{
			imageLocation.x+=w;
		}
		if (imageLocation.y<0)
		{
			imageLocation.y+=h;
		}
		
		//draw the image in four places to cover the screen
		int x = imageLocation.x;
		int y = imageLocation.y;
		g.drawImage(bgImage, x, y,null);
		g.drawImage(bgImage, x-w, y,null);
		g.drawImage(bgImage, x, y-h,null);
		g.drawImage(bgImage, x-w, y-h,null);
		
		//draw instructions
		g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
		g.drawString("Space key changes Mouse Mode. Press Escape to exit",5,FONT_SIZE*2);
		
	
	}

	//uses robot class to try to position the mouse in the center of the scrn
	private synchronized void recenterMouse()
	{
		Window window = screen.getFullScreenWindow();
		if (robot!=null && window.isShowing())
		{
			centerLocation.x = window.getWidth()/2;
			centerLocation.y = window.getHeight()/2;
			SwingUtilities.convertPointToScreen(centerLocation, window);
			isRecentering = true;
			robot.mouseMove(centerLocation.x, centerLocation.y);
		}
	}

	public void mouseDragged(MouseEvent e)
	{
		mouseMoved(e);
	}
	
	public synchronized void mouseMoved(MouseEvent e)
	{
		if (isRecentering && centerLocation.x == e.getX() && centerLocation.y == e.getY())
		{
			isRecentering = false;			
		}
		else
		{
			int dx = e.getX()- mouseLocation.x;
			int dy = e.getY()- mouseLocation.y;
			imageLocation.x += dx;
			imageLocation.y += dy;
			//recenter mouse 
			if (relativeMouseMode)
			{
				recenterMouse();
			}
		}
		
		mouseLocation.x = e.getX();
		mouseLocation.y = e.getY();
	}
	
	public void keyPressed(KeyEvent e)
	{
		int keyCode = e.getKeyCode();
		//exit the program if esc is press
		if (keyCode == KeyEvent.VK_ESCAPE)
		{
			stop();
		}
		else if (keyCode == KeyEvent.VK_SPACE)
		{
			relativeMouseMode = !relativeMouseMode;
			
		}
		
	}
	
	public void keyReleased(KeyEvent e)
	{
	//
	}
	
	public void keyTyped(KeyEvent e)
	{
		//
	}
	
	
	
}
